package co.edu.usbcali.scrum.presentation.backingBeans;

import co.edu.usbcali.scrum.exceptions.*;
import co.edu.usbcali.scrum.modelo.*;
import co.edu.usbcali.scrum.modelo.dto.ProductBacklogDTO;
import co.edu.usbcali.scrum.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.scrum.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class ProductBacklogView {
    private InputText txtEstado;
    private InputText txtCodigoPro_Proyecto;
    private InputText txtCodigoProBack;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ProductBacklogDTO> data;
    private ProductBacklogDTO selectedProductBacklog;

    public ProductBacklogView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ProductBacklogDTO productBacklogDTO = (ProductBacklogDTO) e.getObject();

            if (txtEstado == null) {
                txtEstado = new InputText();
            }

            txtEstado.setValue(productBacklogDTO.getEstado());

            if (txtCodigoPro_Proyecto == null) {
                txtCodigoPro_Proyecto = new InputText();
            }

            txtCodigoPro_Proyecto.setValue(productBacklogDTO.getCodigoPro_Proyecto());

            if (txtCodigoProBack == null) {
                txtCodigoProBack = new InputText();
            }

            txtCodigoProBack.setValue(productBacklogDTO.getCodigoProBack());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtEstado != null) {
            txtEstado.setValue(null);
            txtEstado.setDisabled(true);
        }

        if (txtCodigoPro_Proyecto != null) {
            txtCodigoPro_Proyecto.setValue(null);
            txtCodigoPro_Proyecto.setDisabled(true);
        }

        if (txtCodigoProBack != null) {
            txtCodigoProBack.setValue(null);
            txtCodigoProBack.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        ProductBacklog entity = null;

        try {
            String codigoProBack = new String(txtCodigoProBack.getValue()
                                                              .toString());
            entity = BusinessDelegatorView.getProductBacklog(codigoProBack);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtEstado.setDisabled(false);
            txtCodigoPro_Proyecto.setDisabled(false);
            txtCodigoProBack.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtEstado.setValue(entity.getEstado());
            txtEstado.setDisabled(false);
            txtCodigoPro_Proyecto.setValue(entity.getProyecto().getCodigoPro());
            txtCodigoPro_Proyecto.setDisabled(false);
            txtCodigoProBack.setValue(entity.getCodigoProBack());
            txtCodigoProBack.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveProductBacklog(FacesUtils.checkString(
                    txtCodigoProBack), FacesUtils.checkString(txtEstado),
                FacesUtils.checkString(txtCodigoPro_Proyecto));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteProductBacklog(FacesUtils.checkString(
                    txtCodigoProBack));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateProductBacklog(FacesUtils.checkString(
                    txtCodigoProBack), FacesUtils.checkString(txtEstado),
                FacesUtils.checkString(txtCodigoPro_Proyecto));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCodigoProBack == null) {
                txtCodigoProBack = new InputText();
            }

            txtCodigoProBack.setValue(selectedProductBacklog.getCodigoProBack());

            BusinessDelegatorView.deleteProductBacklog(FacesUtils.checkString(
                    txtCodigoProBack));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedProductBacklog);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String codigoProBack, String estado,
        String codigoPro_Proyecto) throws Exception {
        try {
            BusinessDelegatorView.updateProductBacklog(codigoProBack, estado,
                codigoPro_Proyecto);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ProductBacklogView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtEstado() {
        return txtEstado;
    }

    public void setTxtEstado(InputText txtEstado) {
        this.txtEstado = txtEstado;
    }

    public InputText getTxtCodigoPro_Proyecto() {
        return txtCodigoPro_Proyecto;
    }

    public void setTxtCodigoPro_Proyecto(InputText txtCodigoPro_Proyecto) {
        this.txtCodigoPro_Proyecto = txtCodigoPro_Proyecto;
    }

    public InputText getTxtCodigoProBack() {
        return txtCodigoProBack;
    }

    public void setTxtCodigoProBack(InputText txtCodigoProBack) {
        this.txtCodigoProBack = txtCodigoProBack;
    }

    public List<ProductBacklogDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataProductBacklog();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ProductBacklogDTO> productBacklogDTO) {
        this.data = productBacklogDTO;
    }

    public ProductBacklogDTO getSelectedProductBacklog() {
        return selectedProductBacklog;
    }

    public void setSelectedProductBacklog(ProductBacklogDTO productBacklog) {
        this.selectedProductBacklog = productBacklog;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
